package com.region.a.service;

import com.region.a.service.itf.A1001_ITF;
import com.region.a.service.itf.remote.B1001_ITF;
import com.syf.core.service.SyService;
import com.syf.core.service.SyServiceSetting;
import com.syf.core.service.context.ServiceContext;
import com.syf.core.service.context.ServiceResponse;
import com.syf.kit.soa.core.SyfRemoteServiceInvoker;

@SyServiceSetting(code = "A1001", desc = "test-region-调用")
public class A1001 implements SyService<A1001_ITF.ReqData, A1001_ITF.RespData> {


    @Override
    public void execute(ServiceContext<A1001_ITF.ReqData, A1001_ITF.RespData> ctx) {
        A1001_ITF.ReqData reqData = ctx.getRequest().getDataObj();

        ctx.getTraceLog().info("收到请求，请求报文：{}", reqData);

        // 调用远程regionB交易B1001
        B1001_ITF.ReqData b1001ReqData = new B1001_ITF.ReqData();
        b1001ReqData.setParam1("value1");
        b1001ReqData.setParam2(313);
        b1001ReqData.setParam3(true);

        SyfRemoteServiceInvoker remoteServiceInvoker = ctx.getToolKit(SyfRemoteServiceInvoker.class);
        ServiceResponse<B1001_ITF.RespData> rpcResp = remoteServiceInvoker.callRemoteService("regionB", "B1001", b1001ReqData, B1001_ITF.ReqData.class, B1001_ITF.RespData.class, ctx);
        if (!rpcResp.isSuccess()) {
            ctx.getTraceLog().error("调用远程交易失败，失败错误码：{}，错误描述：{}", rpcResp.getRespCode(), rpcResp.getRespMsg());
            ctx.getResponse().setError("BIZ1002", "调用交易失败：" + rpcResp.getRespMsg());
            return;
        }

        ctx.getResponse().setSuccess();
        ctx.getResponse().setDataObj(new A1001_ITF.RespData("我的返回参数1"));
    }
}
